require 'yaml'
require 'fileutils'

module RCV
  class Config
    HOME = ENV["HOME"] || ENV["AppData"] || "./"
    CONF_DIR = File.join(HOME, ".rcv")
    BOOKMARK_PATH = File.join(CONF_DIR, "bookmark.yaml")

    class << self
      def bookmark=(hash)
        FileUtils.mkpath(CONF_DIR) unless File.exist?(CONF_DIR)
        bm = bookmark_all
        hash.update(bm) { |k, v| v } if bm
        open(BOOKMARK_PATH, "w") do |f|
          f.write hash.to_yaml
        end
      end

      def bookmark(key = nil)
        bm = bookmark_all
        bm[key] if bm && bm.key?(key)
      end

      def clear_bookmark
        File.open(BOOKMARK_PATH, "w").close
      end

      private
      def bookmark_all
        YAML.load(File.read(BOOKMARK_PATH)) if File.exist?(BOOKMARK_PATH)
      end
    end
  end
end
